﻿// <copyright file="EnumConverter.cs" company="Microsoft Corporation">
// Copyright (c) 2008 All Right Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2009-06-15</date>
// <summary>EnumConverter class definition</summary>

namespace NYTimes.ValueConverters
{
    using System;
    using System.Windows;
    using System.Windows.Data;

    /// <summary>
    /// A Value converter to be used to map enumerateded properties to radio buttons or other 
    /// boolean values
    /// </summary>
    /// <example>
    /// From StateZipTotalsPage.xaml where the converter is being used to bind an enumerated value 
    /// in ResourceType to radio buttons and enable/disable text boxes.  Notice how the 
    /// ConverterParameter is being used and that the Mode is TwoWay for RadioButton.IsChecked and 
    /// OneWay for TextBox.IsEnabled.
    /// <code>
    /// <![CDATA[
    /// <RadioButton HorizontalAlignment="Left" VerticalAlignment="Center" Content="State" 
    ///        IsChecked="{Binding ResourceType, Mode=TwoWay, Converter={StaticResource EnumConverter}, ConverterParameter=States}" 
    ///        GroupName="Location"/>
    /// <RadioButton HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Row="1" 
    ///        Content="Zip Code" 
    ///        IsChecked="{Binding ResourceType, Mode=TwoWay, Converter={StaticResource EnumConverter}, ConverterParameter=Zips}" 
    ///        GroupName="Location"/>
    /// <TextBox HorizontalAlignment="Stretch" x:Name="State" VerticalAlignment="Center" 
    ///        Grid.Column="1" Text="{Binding Mode=TwoWay, Path=State}" TextWrapping="Wrap"
    ///        IsEnabled="{Binding ResourceType, Converter={StaticResource EnumConverter}, ConverterParameter=States}"/>
    /// <TextBox HorizontalAlignment="Stretch" x:Name="Zip_Code" VerticalAlignment="Center" 
    ///        Grid.Column="1" Grid.Row="1" Text="{Binding Mode=TwoWay, Path=ZipCode}" 
    ///        TextWrapping="Wrap" 
    ///        IsEnabled="{Binding ResourceType, Converter={StaticResource EnumConverter}, ConverterParameter=Zips}"/>
    /// ]]>
    /// </code>
    /// </example>
    public class EnumConverter : IValueConverter
    {
        #region IValueConverter Members

        /// <summary>
        /// If the value is equivalent to the parameter, then return true
        /// </summary>
        /// <param name="value">an enumerted value</param>
        /// <param name="targetType">The Type of data expected by the target dependency property.</param>
        /// <param name="parameter">a string representation of the enum to compare to</param>
        /// <param name="culture">The culture of the conversion.</param>
        /// <returns>true if the string and the enum value are equivalent</returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            if (parameter == null)
            {
                throw new ArgumentNullException("parameter");
            }

            bool isChecked = value.ToString() == parameter.ToString();

            return isChecked;
        }

        /// <summary>
        /// If the value is true, then parse the parameter and return it as an enum. 
        /// </summary>
        /// <param name="value">a boolean value (true to set, false otherwise)</param>
        /// <param name="targetType">The Type of data expected by the source object.</param>
        /// <param name="parameter">the string value to be converted to an enum and set</param>
        /// <param name="culture">The culture of the conversion.</param>
        /// <returns>if the value is true, then the parsed parameter</returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool checkValue = (bool)value;

            if (checkValue)
            {
                if (targetType == null)
                {
                    throw new ArgumentNullException("targetType");
                }

                if (targetType.IsEnum)
                {
                    if (parameter == null)
                    {
                        throw new ArgumentNullException("parameter");
                    }

                    var parsed = Enum.Parse(targetType, parameter.ToString(), true);

                    return parsed;
                }
            }

            return DependencyProperty.UnsetValue;
        }

        #endregion
    }
}
